<?php
namespace App\Http\Controllers\Admin;
use App\DTOS\Admin\CustomerDTO;
use App\Http\Controllers\Controller;
use App\Models\Game\LoginLogs;
use App\Models\Game\Map;
use App\Utils\MyLog;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ExcelExport;
use App\Utils\ResponseUtil;
use App\DTOS\Admin\LogRoleDeathSearchDTO;
use App\Services\LogRoleDeathService;
// 角色死亡日志
class LogRoleDeathController extends Controller
{
    private LogRoleDeathService $service;

    /**
    * LogRoleDeathService constructor.
    * @param  LogRoleDeathService $service
    */
    public function __construct(LogRoleDeathService $service)
    {
        $this->service = $service;
    }


    public function paginate()
    {
        $DTO = new LogRoleDeathSearchDTO(request()->all());
        $paginate = $this->service->paginate($DTO);
        $data = $paginate->items();
        $res = [];
        $res['code'] = 0;
        $res['data'] = $data;
        $res['count'] = $paginate->total();
        return ResponseUtil::json($res);
    }

    public function export(){ //table 数据导出
        $DTO = new LogRoleDeathSearchDTO(request()->all());
        $DTO->setLimit(99999);
        $map=Map::query()->select('id','name')->get();
        $mapNameMap = [];
        foreach ($map->pluck('name', 'id')->toArray() as $id => $name) {
            $mapNameMap[$id] = $name;
        }
        $paginate = $this->service->paginate($DTO);
        $excelRows = [
            ['日志生产时间','所在线','所在服务器ID','账号ID','角色ID','角色昵称','击杀者类型','击杀者ID',"击杀者名称","场景"]
        ];
        $killTypeMap = [
            1 => "玩家",
            0 => "NPC"
        ];
        foreach($paginate->items() as $datum){
            $killType = $killTypeMap[$datum['kill_type']] ?? "未知";

            $excelRows[] = [
                date("Y-m-d H:i:s", $datum['logTime']),$datum['line'],$datum['serverId'],$datum['memId'],$datum['roleId'],$datum['nick'],$killType,
                strval($datum['kill_id']),$datum['kill_nick'] ,    $mapNameMap[$datum['map_id']] ?? "未知",
            ];
        }
        return ResponseUtil::success(cacheDownload(arrayToScvContent($excelRows),'.死亡日志.csv'));

    }




}
